<template>
  <div>
    <el-button type="primary" class="marginBottom10" @click="isAdd = true; showAddBox = true; cleanForm()">新增</el-button>
    <el-table :data='tableData' style='width: 100%'>
      <el-table-column type="index" label='序号' ></el-table-column>
      <el-table-column prop='createtime' sortable label='创建时间' ></el-table-column>
      <el-table-column prop='img' label='banner图'>
        <template slot-scope="scope">
          <img :src="scope.row.img" width="100"/>
        </template>
      </el-table-column>
      <el-table-column prop='status' label='状态'>
        <template slot-scope="scope">
          <!-- 是否展示【0-下架，1-展示】 -->
          <span v-if="scope.row.status === '0'">下架</span>
          <span v-if="scope.row.status === '1'">展示</span>
        </template>
      </el-table-column>
      <el-table-column label='操作'>
        <template slot-scope="scope">
          <span class="astyle"  @click="editItem(scope.row)">编辑</span>
          <span class="dstyle"  @click="deleteItem(1, scope.row.id)">删除</span>
        </template>
      </el-table-column>
    </el-table>
    <div class="page">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.page"
        :page-sizes="[20, 50, 100]"
        :page-size="page.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total">
      </el-pagination>
    </div>
    <el-dialog
      :close-on-click-modal="false"
      :title="isAdd ? '新增' : '编辑'"
      :visible.sync="showAddBox"
      >
      <el-form :model="diaForm" :rules="rules"  ref="bannerForm" label-width="110px">

        <el-form-item label="banner图" prop="img" style="width: 500px;">
          <el-upload
            class="avatar-uploader"
            action="#"
            :show-file-list="false"
            :before-upload="beforeUpload">
            <img v-if="diaForm.img" :src="diaForm.img" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="diaForm.status">
            <el-radio label="0">下架</el-radio>
            <el-radio label="1">展示</el-radio>
          </el-radio-group>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showAddBox = false">取 消</el-button>
        <el-button type="primary" @click="handleClickSendForm('bannerForm')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: 'BannerSetting',
  data () {
    return {
      isAdd: false,
      showAddBox: false,
      diaForm: {
        id: '',
        img: '',
        status: ''
      },
      tableData: [],
      page: {
        page: 1,
        size: 20,
        total: 1
      },
      rules: {
        img: [
          { required: true, message: '请选择banner图', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '请选择banner展示状态', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    getList () {
      let params = {
        page: this.page.page,
        size: this.page.size
      }
      this.axios.get('/Luckdraw/getbanner', { params }).then(res => {
        if (res.data.status === 1 || res.data.status === '1') {
          this.page.total = Number(res.data.data.count)
          this.tableData = res.data.data.list
        }
      })
    },
    handleSizeChange (val) {
      this.page.size = val
      this.getList()
    },
    handleCurrentChange (val) {
      this.page.page = val
      this.getList()
    },
    editItem (row) {
      let self = this
      for (let key in this.diaForm) {
        self.diaForm[key] = row[key]
      }
      this.isAdd = false
      this.showAddBox = true
    },
    deleteItem (e = 1, val) {
      let self = this
      let params = {}
      params.id = val
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.axios.get('/Luckdraw/delbanner', { params: {id: val} }).then(res => {
          if (res.data.status === 1 || res.data.status === '1') {
            self.$message({
              message: '删除成功',
              type: 'success'
            })
            self.getList()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    cleanForm () {
      let self = this
      for (let key in self.diaForm) {
        self.diaForm[key] = ''
      }
    },
    beforeUpload (file) {
      let reader = new FileReader()
      let self = this
      reader.onload = function (e) {
        self.diaForm.img = e.currentTarget.result
      }
      reader.readAsDataURL(file)
      return false
    },
    handleClickSendForm (formName) {
      let url = this.isAdd ? '/Luckdraw/addbanner' : '/Luckdraw/editbanner'
      let self = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let params = {}
          for (let key in self.diaForm) {
            if (key !== 'id') {
              if (self.diaForm[key] !== '') {
                params[key] = self.diaForm[key]
              }
            }
          }
          if (!self.isAdd) {
            params['id'] = self.diaForm.id
          }
          this.axios.post(url, self.qs.stringify(params)).then(res => {
            if (res.data.status === 1 || res.data.status === '1') {
              self.$message({
                message: '成功',
                type: 'success'
              })
              self.showAddBox = false
              self.getList()
            }
          })
        } else {
          return false
        }
      })
    }
  },
  mounted () {
    this.getList()
  }
}
</script>
<style scoped>
@import "../../../assets/css/style.css";
</style>
